package com.xiaoy.core.basic.organization.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.xiaoy.core.basic.security.entity.PartyRole;
import com.xiaoy.util.store.config.ConfigTable;
@Entity
@Table(name=ConfigTable.Basic.PARTY)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@SequenceGenerator(name=ConfigTable.Basic.PARTY+"_SEQ",sequenceName=ConfigTable.Basic.PARTY+"_SEQ",allocationSize=1)
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)  
public class Party {
	
	private Party parent;
	private List<Party>children;
	
	private PartyType partyType;
	
	private String name;
	
	private long id;
	
	private long sort;
	
	private long priority;
	
	private List<PartyRole> partyRoles = new ArrayList<PartyRole>();
	
	private long createuser;
	private long updateuser;
	private Date createdate;
	private Date updatedate;
	
	public Party () {}
	
	public Party (String name) {
		this.name=name;
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BAS_ORG_PARTY_SEQ")
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "pid")
	public Party getParent() {
		return parent;
	}

	public void setParent(Party parent) {
		this.parent = parent;
	}

	@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "parent")
	@Fetch(FetchMode.SUBSELECT)
	public List<Party> getChildren() {
		return children;
	}

	public void setChildren(List<Party> children) {
		this.children = children;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "typeid")
	public PartyType getPartyType() {
		return partyType;
	}

	public void setPartyType(PartyType partyType) {
		this.partyType = partyType;
	}

	public long getCreateuser() {
		return createuser;
	}

	public void setCreateuser(long createuser) {
		this.createuser = createuser;
	}

	public long getUpdateuser() {
		return updateuser;
	}

	public void setUpdateuser(long updateuser) {
		this.updateuser = updateuser;
	}

	public Date getCreatedate() {
		return createdate;
	}

	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}

	public Date getUpdatedate() {
		return updatedate;
	}

	public void setUpdatedate(Date updatedate) {
		this.updatedate = updatedate;
	}

	public long getSort() {
		return sort;
	}

	public void setSort(long sort) {
		this.sort = sort;
	}

	public long getPriority() {
		return priority;
	}

	public void setPriority(long priority) {
		this.priority = priority;
	}

	@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "party")
	@Fetch(FetchMode.SUBSELECT)
	public List<PartyRole> getPartyRoles() {
		return partyRoles;
	}

	public void setPartyRoles(List<PartyRole> partyRoles) {
		this.partyRoles = partyRoles;
	}
}
